#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    time(&startTime);
    //QString path1("/etc");
    dir.setCurrent("C:\\");
    fileInfoList = dir.entryInfoList(QDir::AllEntries, QDir::DirsFirst);
    i = 0;
    char *path = "FIFO.tmp";
//    mkfifo(path, 0777);
//    fd = open(path, O_WRONLY, 0);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(printFiles()));
    ui->setupUi(this);
    ui->textEdit->append("Type\tName\t\t\tSize\n");
    timer->start(1000);
}

Dialog::~Dialog()
{
    delete ui;
//    close(fd);
}

void Dialog::printFiles()
{
    int num = dir.count();
    if(i<num)
    {
        if(fileInfoList[i].isDir())
        {
            ui->textEdit->append("[Dir]\t"+fileInfoList[i].fileName());
        }else
        {
            ui->textEdit->append("[File]\t"+fileInfoList[i].fileName()+"\t\t\t"+QString::number(fileInfoList[i].size(), 10)+"Bytes");
        }
        //ui->textEdit->moveCursor(QTextCursor::MoveOperation, QTextCursor::MoveAnchor);
        i++;
       // ui->textEdit->append(QString(num, 10));
    }
    else
    {
        timer->stop();
    }
//    emit Dialog::printing();
}

void Dialog::IPC(int fd, time_t i)
{
//    int fd;
//    mkfifo(path, O_CREAT|O_RDWR);
//    fd = open(path, O_WRONLY, 0);
//    write(fd, &i, sizeof(time_t));
//    close(fd);
}
